<?php
/**
 * Created by PhpStorm.
 * User: henry
 * Date: 2017/2/23
 * Time: 上午10:12
 */

namespace business\base\models;

/**
 * Class BspShowForestModel   基础分类--对外展示分类表
 * @package business\base\models
 * @property string categoryId 数据库主键
 * @property string categoryShortname 分类简称
 * @property string categoryName 分类全称
 * @property string categoryCode 分类编码，内部唯一
 * @property string parentCode 父级分类编码
 * @property string isLeaf 是否末级.1末级，0非末级
 * @property string hierarchyLevel 级次
 * @property string categoryStatus 状态:1启用，0停用
 * @property string displayOrder 显示顺序
 * @property string basicForestCode 基础分类编码
 * @property string directoryName 目录名称
 * @property string title 标题
 * @property string description 描述
 * @property string keywords 关键字
 * @property string createdTime  创建时间
 * @property string lastModifiedTime 最后修改时间,精确到时分秒
 * @property string createdBy 创建人
 * @property string lastModifiedBy 最后修改人
 * @property BspShowForestModel[] $childCategoryList 最后修改人
 */
class BspShowForestModel extends BaseActiveRecord
{
    /**
     * @var array 子分类集合
     */
    public $childCategoryList;
    /**
     * @var array 排序字段集合
     */
    public $sortFieldValueList;

    public static function tableName()
    {
        return 'b2b_parameter.bsp_show_forest';
    }

    public $_fieldsAlias = [
        'categoryId' => 'category_id',
        'categoryShortname' => 'category_shortname',
        'categoryName' => 'category_name',
        'categoryCode' => 'category_code',
        'parentCode' => 'parent_code',
        'isLeaf' => 'is_leaf',
        'hierarchyLevel' => 'hierarchy_level',
        'categoryStatus' => 'category_status',
        'displayOrder' => 'display_order',
        'basicForestCode' => 'basic_forest_code',
        'directoryName' => 'directoryName',
        'title' => 'title',
        'description' => 'description',
        'keywords' => 'keywords',
        'createdTime' => 'created_time',
        'lastModifiedTime' => 'last_modified_time',
        'createdBy' => 'created_by',
        'lastModifiedBy' => 'last_modified_by',
    ];

    public function loadAttr($data)
    {
        if (!empty($data)) {
            if (is_array($data)) {
                foreach ($data as $name => $value) {
                    //只有别名字段、列字段、以及public属性，才能赋值
                    $this->$name = $value;
                }
            }
            return true;
        } else {
            return false;
        }
    }


    /**
     * 获取表字段的列名列表
     * @return array
     */
    public function tableColumns()
    {
        return parent::attributes();
    }

    public function convertToArray()
    {
        $data = [];
        //数据库字段属性
        foreach ($this->_fieldsAlias as $key => $value) {
            if (isset($this->$key)) {
                $data[$key] = $this->$key;
            }
        }
        //public 属性
        $data["sortFieldValueList"] = $this->sortFieldValueList;

        //childCategoryList属性转换处理
        if (!empty($this->childCategoryList)) {
            $childCategoryList = [];
            foreach ($this->childCategoryList as &$item) {
                array_push($childCategoryList, $item->convertToArray());
            }
            $data["childCategoryList"] = $childCategoryList;
        }
        return $data;
    }
}